package com.vogue.supplymall.shop.domain.service.impl;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.common.domain.dto.RelevanceInfo;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.order.domain.service.CommentService;
import com.vogue.supplymall.order.domain.service.OrderItemProductService;
import com.vogue.supplymall.shop.common.CodeTransformUtils;
import com.vogue.supplymall.shop.domain.dao.RelevanceDao;
import com.vogue.supplymall.shop.domain.model.Relevance;
import com.vogue.supplymall.shop.domain.service.RelevanceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Relevance service.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-03-28
 */
@Service("relevanceService")
public class RelevanceServiceImpl extends BaseServiceImpl<Relevance, Long> implements RelevanceService {
    @Resource
    private RelevanceDao relevanceDao;
    @Resource
    private CodeTransformUtils codeTransformUtils;
    @Resource
    private OrderItemProductService orderItemProductService;
    @Resource
    private CommentService commentService;


    /**
     * Instantiates a new Relevance service.
     *
     * @param baseDao the base dao
     */
    public RelevanceServiceImpl(BaseDao<Relevance, Long> baseDao) {
        super(baseDao);
    }

    @Override
    public Relevance addRelevance(RelevanceInfo relevanceInfo) {
        List<Relevance> result = relevanceDao.findRelevance(relevanceInfo);
        if (result.size() > 0) {
            return result.get(0);
        }
        Relevance relevance = new Relevance();
        relevance.setType(relevanceInfo.getType());
        relevance.setOriginobjid(relevanceInfo.getUserId());
        /*源对象暂时只是用户（买家）*/
        relevance.setOriginobjtype(1);
        relevance.setTargetobjtype(relevanceInfo.getTargetObjType());
        relevance.setTargetobjid(relevanceInfo.getTargetObjId());
        relevance.setTargetname(relevanceInfo.getTargetName());
        relevance.setDisabledflag(false);
        return relevanceDao.save(relevance);
    }

    @Override
    public List<Map<String, Object>> searchProductWatch(RelevanceInfo relevanceInfo) {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setType(relevanceInfo.getType());
        searchInfo.setLoginId(relevanceInfo.getUserId());
        searchInfo.setSyscodeid(relevanceInfo.getSyscodeid());
        searchInfo.setStart(relevanceInfo.getStart());
        searchInfo.setLength(relevanceInfo.getLength());
        if (StringUtils.isNotEmpty(relevanceInfo.getTargetName())) {
            searchInfo.setContent(relevanceInfo.getTargetName());
        }
        searchInfo.setTargetObjType(relevanceInfo.getTargetObjType());

        return relevanceDao.findProductWatch(searchInfo);
    }

    @Override
    public List<Map<String, Object>> searchShopWatch(RelevanceInfo relevanceInfo) {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setType(relevanceInfo.getType());
        searchInfo.setLoginId(relevanceInfo.getUserId());
        searchInfo.setSyscodeid(relevanceInfo.getSyscodeid());
        searchInfo.setStart(relevanceInfo.getStart());
        searchInfo.setLength(relevanceInfo.getLength());
        if (StringUtils.isNotEmpty(relevanceInfo.getTargetName())) {
            searchInfo.setContent(relevanceInfo.getTargetName());
        }
        searchInfo.setTargetObjType(relevanceInfo.getTargetObjType());
        return codeTransformUtils.searchArea(relevanceDao.findShopWatch(searchInfo));
    }

    @Override
    public Map<String, Object> countRelevance(RelevanceInfo relevanceInfo) {
        Map<String, Object> result = new HashMap<>();
        result.putAll(relevanceDao.countWatch(relevanceInfo));//商品关注
        result.putAll(relevanceDao.countCollect(relevanceInfo));//店铺关注
        result.putAll(commentService.getCommentingCount(relevanceInfo));//去评价(待评价)数量
        return result;
    }

    @Override
    public Boolean removeWatch(RelevanceInfo relevanceInfo) {
        Relevance relevance = relevanceDao.selectOneByWhr("id="+relevanceInfo.getId());
        if (null == relevance) {
            throw new DataNotFindException("");
        }
        relevance.setDisabledflag(true);
        try {
            relevanceDao.update(relevance);
        } catch (RuntimeException r) {
            throw new DataNotFindException("");
        }
        return true;
    }

    @Override
    public Boolean removeWatchs(RelevanceInfo relevanceInfo) {
        for (java.lang.Long id : relevanceInfo.getIds()) {
            Relevance relevance = relevanceDao.selectOneByWhr("id="+relevanceInfo.getId());
            if (null == relevance) {
                throw new DataNotFindException("");
            }
            relevance.setDisabledflag(true);
            try {
                relevanceDao.update(relevance);
            } catch (RuntimeException r) {
                throw new DataNotFindException("");
            }
        }
        return true;
    }
}
